package cst.a110lyj.startproject110;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import cst.a110lyj.startproject110.bean.WeatherInfo;
import cst.a110lyj.startproject110.bean.WeatherResult;
import cst.a110lyj.startproject110.listener.WeatherListener;
import cst.a110lyj.startproject110.model.Weather51Model;
import cst.a110lyj.startproject110.model.WeatherModel;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,WeatherListener {
    private EditText cityid;
    private TextView city, temp;
    private TextView wd;
    private TextView ws;
    private Button button1;
    private Button button2;

    void init() {
        cityid = (EditText) findViewById(R.id.cityid);
        city = (TextView) findViewById(R.id.city);
        temp = (TextView) findViewById(R.id.temp);
        wd = (TextView) findViewById(R.id.WD);
        ws = (TextView) findViewById(R.id.WS);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button1);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                WeatherModel weatherModel = new WeatherModel();
                weatherModel.getWeather(cityid.getText().toString(), this);
            case R.id.button2:
                Weather51Model weather51Model = new Weather51Model();
                weather51Model.getWeather(cityid.getText().toString(), this);
        }
    }

    @Override
    public void onResponse(WeatherInfo w) {
        if(w!=null){
            city.setText(w.getCity().toString());
            temp.setText(w.getTemp().toString());
            wd.setText(w.getWD());
            ws.setText(w.getWS());
        }else {
            city.setText("Error!");
            temp.setText("Error!");
        }
    }

    @Override
    public void onFail(String msg) {
        System.out.println(msg);
    }
}